<template>
  <div>
    <!-- 未登录 -->
    <div class="nologin header" v-if="!user">
      <img @click="$router.push('/login')" src="@/assets/mobile.png">
      <span @click="$router.push('/login')">登录 / 注册</span>
    </div>
    <!-- 已登录 -->
    <div class="logining header" v-else>
     <div class="up">
        <div class="left">
        <van-image
          round
          :src="userInfo.photo"
        />
        <span>{{userInfo.name}}</span>
      </div>
      <div class="rigiht">
        <van-button to="/user" type="default">编辑资料</van-button>
      </div>
     </div>
       <ul class="down">
         <li>
           <span>{{ userInfo.art_count }}</span>
           <span>粉丝</span>
         </li>
         <li>
           <span>{{ userInfo.follow_count }}</span>
           <span>粉丝</span>
         </li>
         <li>
           <span>{{ userInfo.fans_count }}</span>
           <span>粉丝</span>
         </li>
         <li>
           <span>{{ userInfo.like_count }}</span>
           <span>粉丝</span>
         </li>
       </ul>
    </div>
    <!-- 收藏历史 -->
    <van-grid :column-num="2">
      <van-grid-item>
        <i slot="icon" class="toutiao toutiao-shoucang"></i>
        <span slot="text" class="text">收藏</span>
      </van-grid-item>
      <van-grid-item>
        <i slot="icon" class="toutiao toutiao-lishi"></i>
        <span slot="text" class="text">历史</span>
      </van-grid-item>
    </van-grid>
    <!-- 小智同学什么的 -->
    <div class="cellLink">
      <van-cell title="消息通知" is-link />
      <van-cell title="小智同学" is-link />
    </div>
    <!-- 退出 -->
      <div v-if="user" class="loginout">
        <van-cell @click="onLoginout" title="退出登录"/>
      </div>
  </div>
</template>

<script>
import { mapState } from 'vuex'
import { getUserInfo } from '@/api/user.js'

export default {
  name: 'Profile',
  data () {
    return {
      userInfo: {}
    }
  },

  created () {
    if (this.user) {
      this.loadUserInfo()
    }
  },

  computed: {
    ...mapState(['user'])
  },

  methods: {
    // 退出登录
    onLoginout () {
      this.$dialog.confirm({
        title: '黑马头条',
        message: '是否确认退出登录?'
      }).then(() => {
        this.$store.commit('setUser', null)
        this.$toast('退出成功')
      }).catch(() => {})
    },
    async loadUserInfo () {
      try {
        const { data: res } = await getUserInfo()
        this.userInfo = res.data
        console.log(res.data)
      } catch (e) {
        this.$toast('获取信息失败，请稍后再试!')
      }
    }
  }
}
</script>

<style scoped lang='less'>
.header {
  width: 100%;
  height: 401px;
  background: url(~@/assets/banner.png);
  background-size: contain;
}
.nologin {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  img {
    width: 132px;
    height: 132px;
    margin-bottom: 15px;
  }
  span {
    font-size: 28px;
    color: #FFF;
  }
}
.logining {
  .up{
    box-sizing: border-box;
    height: 240px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 50px 32px 0;
    .left {
      display: flex;
      align-items: center;
      .van-image {
        width: 132px;
        height: 132px;
      }
      span {
        margin-left: 15px;
        color: #fff;
        font-size: 30px;
      }
    }
    .rigiht {
      .van-button {
        width: 140px;
        height: 40px;
        border-radius: 20px;
        background-color: #fff;
        padding: 2px 5px;
      }
    }
  }
  .down {
    height: 161px;
    display: flex;
    color: #fff;
      li {
        flex: 1;
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        span:nth-child(1) {
          font-size: 36px;
          margin-bottom: 20px;
        }
        span:nth-child(2) {
          font-size: 23px;
        }
      }
  }
}

.van-grid {
  i {
    font-size: 45px;
  }
  .toutiao-shoucang {
      color: #EB5253;
    }
    .toutiao-lishi {
      color: #FF9D1D;
    }
  .text {
    font-size: 28px;
    margin-top: 15px;
  }
}
.cellLink {
  margin: 10px 0;
  .van-cell:nth-child(1) {
    margin-bottom: 5px;
  }
}
.loginout {
  .van-cell {
    text-align: center;
    color: #D86262;
  }
}
</style>
